<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
$('a[data-typeid="20"]').addClass("am-btn-primary");
var queryParams = {};
var tables = $('#dataTables').DataTable({
    "sAjaxSource" : '{{ urlfor "GspController.Neareffect"}}'
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        queryParams = checkQueryTableData(aoData);
        var form = $('.query-form').serializeArray();
        $.each(form, function(i, value){
            if(value.name == 'typeid') {
                queryParams[value.name] = Number(value.value);
            } else {
                queryParams[value.name] = value.value;
            }
        });
        $.post(sSource, JSON.stringify(queryParams),function(resp) {
            isLoading = true;
            fnCallback(resp);
        },'json')
    }
    ,order:[]
    ,columns: [
        {render:function(data, type, full, meta) {
                return productTypeidName(full.Products.TypeId);
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
            return full.Products.BrandName;
        }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.Name;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.ColourNumber;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.Spec;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.CastAway;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.ShelfLife;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.RegistrationId;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.Unit;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Snd.sph;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Snd.cyl;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Acceptance.lot_number;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return timetransDate(full.Acceptance.manufacture_date);
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return timetransDate(full.Acceptance.expiry_date);
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return monthDayDiff(timetransDate(Date.parse(new Date())/1000), timetransDate(full.Acceptance.expiry_date))+'个月';
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.Manufacturer;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.Products.ProducingArea;
            }, "bSortable": false}
        ,{render:function(data, type, full, meta) {
                return full.purchase_num;
            }, "bSortable": false}
    ]
    ,"aLengthMenu":[20, 50, 100, 200]
	,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
});

$("#query-submit").bind("click", function () { //点击按钮 触发table重新请求服务器
    $("#dataTables").dataTable().fnDraw(false);
    return false;
});
function monthDayDiff(startDate, endDate) {
    let flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2];
    let start = new Date(startDate);
    let end = new Date(endDate);
    let year = end.getFullYear() - start.getFullYear();
    let month = end.getMonth() - start.getMonth();
    let day = end.getDate() - start.getDate();
    if (month < 0) {
        year--;
        month = end.getMonth() + (12 - start.getMonth());
    }
    if (day < 0) {
        month--;
        let index = flag.findIndex((temp) => {
            return temp === start.getMonth() + 1
        });
        let monthLength;
        if (index <= 6) {
            monthLength = 31;
        } else if (index > 6 && index <= 10) {
            monthLength = 30;
        } else {
            monthLength = 28;
        }
        day = end.getDate() + (monthLength - start.getDate());
    }
    return 12 * year + month;
}
</script>